<?php

include('../../include/functions.inc.php');
//echo getParam('operation');
if (!isEmpty(getParam('operation'))) {
    switch (getParam('operation')) {
        case 'assigns':
            getAssigns();
            break;
        case 'detail':
            getCoursesAssign();
            break;
        case 'cancel':
            cancelAssign($db);
            break;
        case 'cancelcourse':
            cancelAssignDetail($db);
            break;
        case 'delete':
            deleteAssign($db);
            break;
        case 'deletecourse':
            deleteAssignDetail($db);
            break;
        case 'add':
            addAssignDetail($db);
            break;
    }
}

//carga pagina de asignaciones maestras
function getAssigns() {

    header("Location: assigns.php?ID=" . getParam("ID"));
}

//carga pagina de detalle de asignaciones
function getCoursesAssign() {
    //  echo "LLEGO...";
    header("Location: assignsdetail.php?ID=" . getParam("ID"));
}

function cancelAssign($db) {
    $db->query_update("cllg_student_assign", array("STATUS" => sqlValue(0, "int")), "ID=" . sqlValue(getParam("ID"), "int"));
    $db->query_update("cllg_assign_detail", array("STATUS" => sqlValue(0, "int")), "FK_ASSIGN=" . sqlValue(getParam("ID"), "int"));

    $sql = "SELECT  fk_account, 
                    SUM(amount) total 
            FROM    cllg_cargo 
            WHERE   fk_student_assign = " . sqlValue(getParam("ID"), "int");
    $arrInfoCargoAsig = array();
    $arrInfoCargoAsig = $db->fetch_all_array($db->query($sql));
    $arrInfoCargoAsig = $arrInfoCargoAsig[0];

    $sql = "SELECT  curr_balance, 
                    prev_balance 
            FROM    cllg_account 
            WHERE   id = " . $arrInfoCargoAsig['fk_account'];
    $arrInfoAccount = array();
    $arrInfoAccount = $db->fetch_all_array($db->query($sql));
    $arrInfoAccount = $arrInfoAccount[0];

    $sinCurrBalance = $arrInfoAccount['curr_balance'] - $arrInfoCargoAsig['total'];
    $sinPrevBalance = $arrInfoAccount['prev_balance'] - $arrInfoCargoAsig['total'];

    $sinCurrBalance = ($sinCurrBalance < 0 ) ? 0 : $sinCurrBalance;
    $sinPrevBalance = ($sinPrevBalance < 0 ) ? 0 : $sinPrevBalance;
    
    $db->query_update('cllg_account', array('CURR_BALANCE' => $sinCurrBalance,
        'PREV_BALANCE' => $sinPrevBalance), 'ID = ' . $arrInfoCargoAsig['fk_account']);
    $db->query_delete("cllg_cargo", "FK_STUDENT_ASSIGN = " . sqlValue(getParam("ID"), "int"));
    $_SESSION['pack'] = serialize(array("MSG" => "Asignacion Cancelada", "sql" => $db->lastQuery));
    header('Location: ../../include/transport.php');
}

function cancelAssignDetail($db) {
    $db->query_update("cllg_assign_detail", array("STATUS" => sqlValue(0, "int")),"ID=" . sqlValue(getParam("ID_DET_ASSIGN"), "int"));
    
    
    $_SESSION['pack'] = serialize(array("ST" => 1, "MSG" => "Asignacion de curso anulada"));
    header('Location: ../../include/transport.php');
}

function deleteAssign($db) {
    $db->query_delete("cllg_assign_detail", "FK_ASSIGN=" . sqlValue(getParam("ID"), "int"));
    $db->query_delete("cllg_student_assign", "ID=" . sqlValue(getParam("ID"), "int"));
    
    $sql = "SELECT  fk_account, 
                    SUM(amount) total 
            FROM    cllg_cargo 
            WHERE   fk_student_assign = " . sqlValue(getParam("ID"), "int");
    $arrInfoCargoAsig = array();
    $arrInfoCargoAsig = $db->fetch_all_array($db->query($sql));
    $arrInfoCargoAsig = $arrInfoCargoAsig[0];

    $sql = "SELECT  curr_balance, 
                    prev_balance 
            FROM    cllg_account 
            WHERE   id = " . $arrInfoCargoAsig['fk_account'];
    $arrInfoAccount = array();
    $arrInfoAccount = $db->fetch_all_array($db->query($sql));
    $arrInfoAccount = $arrInfoAccount[0];

    $sinCurrBalance = $arrInfoAccount['curr_balance'] - $arrInfoCargoAsig['total'];
    $sinPrevBalance = $arrInfoAccount['prev_balance'] - $arrInfoCargoAsig['total'];

    $sinCurrBalance = ($sinCurrBalance < 0 ) ? 0 : $sinCurrBalance;
    $sinPrevBalance = ($sinPrevBalance < 0 ) ? 0 : $sinPrevBalance;

    $db->query_update('cllg_account', array('CURR_BALANCE' => $sinCurrBalance,
        'PREV_BALANCE' => $sinPrevBalance), 'ID = ' . $arrInfoCargoAsig['fk_account']);
    $db->query_delete("cllg_cargo", "FK_STUDENT_ASSIGN = " . sqlValue(getParam("ID"), "int"));
    $_SESSION['pack'] = serialize(array("MSG" => "Asignacion Eliminada", "sql" => $db->lastQuery));
    header('Location: ../../include/transport.php');
}

function deleteAssignDetail($db) {
    $db->query_delete("cllg_assign_detail", "ID=" . sqlValue(getParam("ID_DET_ASSIGN"), "int"));
    
    $_SESSION['pack'] = serialize(array("ST" => 1, "MSG" => "Asignacion de curso eliminada"));
    header('Location: ../../include/transport.php');
}

function addAssignDetail($db) {
    $db->query_insert("cllg_assign_detail", array("FK_ASSIGN" => getParam("ID_ASSIGN"),
        "FK_COURSE" => getParam("CODE"),
        "SECTION" => sqlValue(getParam("SECTION"), "text")));

    $_SESSION['pack'] = serialize(array("ST" => 1, "MSG" => "Curso Asignado"));
    header('Location: ../../include/transport.php');
}

?>